<?php
if (!defined('IN_MEDIA')) die("Hacking attempt");
$m_per_page = m_get_config('media_per_page');
$fields = "m_id, m_title, m_rating, m_rating_total, m_cat, m_rating_week, m_rating_total_week, m_singer, m_poster, m_type, m_viewed, m_downloaded, m_bitrate, m_playtime, IF(m_lyric = '' OR m_lyric IS NULL,0,1) m_lyric";
$q = '';

if($show == 'list') {
$sql_order = "ORDER BY m_id DESC";
$link = (m_get_config('url_type') == 0)?$mainURL."/index.php?pg=":$mainURL."/Danh-sach-bai-hat/trang";
	if(is_numeric($cat_id)) {
		$sql_where = "WHERE m_cat = $cat_id";
		$sql_order = "ORDER BY m_id DESC";
		$title =  m_get_data('CAT',$cat_id);
		$link = (m_get_config('url_type') == 0)?$mainURL."/index.php?go=list&show=list&cat_id=".$cat_id."&pg=":$mainURL."/Cat/".$cat_id."/".name_on_bar(m_get_data('CAT',$cat_id))."/Page/";
		$num = $mysql->query("SELECT * FROM ".$tb_prefix."data $sql_where");
		$total = $mysql->num_rows($num);
		if (!$pg) $pg = 1;
		$m_per_page = m_get_config("media_per_page");
		$sql = "SELECT ".$fields." FROM ".$tb_prefix."data $sql_where $sql_order LIMIT ".(($pg-1)*$m_per_page).",".$m_per_page;
	}
	else if($order == 'downloaded_week') {
		$sql_where = "WHERE m_$order > 0";
		$sql_order = "ORDER BY m_$order DESC";
		$link = (m_get_config('url_type') == 0)?$mainURL."/index.php?go=list&show=list&order=".$order."&pg=":$mainURL."/Top/download-trong-tuan/Page/";
		$title = "Top tải nhiều trong tuần";
		$sql = "SELECT ".$fields." FROM ".$tb_prefix."data $sql_where $sql_order LIMIT 40";
	}

	else if($order == 'rating_total_week')
	{
		
		$sql_where = "WHERE m_$order > 0";
		$sql_order = "ORDER BY m_$order DESC";
		$title = "Top bình chọn trong tuần";
		$link = (m_get_config('url_type') == 0)?$mainURL."/index.php?go=list&show=list&order=".$order."&pg=":$mainURL."/Top/binh-chon-trong-tuan/Page/";
		$sql = "SELECT ".$fields." FROM ".$tb_prefix."data $sql_where $sql_order LIMIT 40";

	}	
	else if($order == 'viewed_week')
	{
		$sql_where = "WHERE m_$order > 0";
		$sql_order = "ORDER BY m_$order DESC";
		$title = "Top nghe nhiều trong tuần";
		$link = (m_get_config('url_type') == 0)?$mainURL."/index.php?go=list&show=list&order=".$order."&pg=":$mainURL."/Top/nghe-trong-tuan/Page/";
		$sql = "SELECT ".$fields." FROM ".$tb_prefix."data $sql_where $sql_order LIMIT 40";

	}
	
	// QUERY SQL

		
	else{
	$num = $mysql->query("SELECT * FROM ".$tb_prefix."data $sql_where");
	$total = $mysql->num_rows($num);
	if (!$pg) $pg = 1;
	$m_per_page = m_get_config("media_per_page");
	$sql = "SELECT ".$fields." FROM ".$tb_prefix."data $sql_where $sql_order LIMIT ".(($pg-1)*$m_per_page).",".$m_per_page;
	}
	
	$i = 1;
	$query = $mysql->query($sql);
	if($mysql->num_rows($query)) {
		while($row = $mysql->fetch_array($query)) {
			switch ($row['m_type']) {
				case 1 : $media_type = 'music'; break;
				case 2 : $media_type = 'flash'; break;
				case 3 : $media_type = 'movie'; break;
			}
			$song_TYPE = "<img src='$tpl_link/img/media/type/$media_type.gif'>";
			$song_LYRIC = ($row_song['m_lyric'])?"<img src='$tpl_link/img/media/ok.gif'>":'';
			$song_ID = $row['m_id'];
			/*
			 * get sumlike 1 bai hat.
			*
			*/
			$sqls = "SELECT count(*) as sumlike FROM ".$tb_prefix."post WHERE post_media_id =" . $song_ID;
			$s = $mysql->fetch_array($mysql->query($sqls));
			$sumlike = $s['sumlike'];
			//end $sumlike ---------------------------------------------------------
			
			$user_ID = $row['m_poster'];
			$user_NAME =  m_get_data('USER',$user_ID);
			if($user_NAME == '')
				{
					$user_NAME = 'Other';
				}

			$song_TITLE = $row['m_title'];
			$singer_ID = $row['m_singer'];
			$singer_NAME =  m_get_data('SINGER',$singer_ID);
			$song_VIEWED = $row['m_viewed'];
			$song_DOWNLOADED = $row['m_downloaded'];
			$m_bitrate = $row['m_bitrate'];
			$m_playtime = $row['m_playtime'];
			$cat_ID =  $row['m_cat'];
			$cat_NAME =  m_get_data('CAT',$cat_ID);
			$cat_URL    = url_link($cat_ID,$cat_NAME,'Cat');			
			$song_URL = url_link($song_ID,$song_TITLE,'nhac-san');
			$singer_URL = url_link($singer_ID,$singer_NAME,'Singer');
			$view_pages = m_viewpages($total,$m_per_page,$pg,$link);
			$song_TITLE1 = $song_TITLE;
			if($user_NAME == '')
				{
					$user_NAME = 'DJ Other';
				}
			$addp = 'addToPlaylist('.$song_ID.');return false;';	
			if (!$isLoggedIn){
				$addp = 'alert("Yêu cầu đăng nhập")';
			}
			$user_URL	= url_link($user_ID,$user_NAME,'User');
			$m_rate = $row['m_rating'];
			$m_rate_total = $row['m_rating_total'];
			if ($m_rate =='0') $current_star = 0;

			else $rater_rating = $m_rate / $m_rate_total;
			if ($rater_rating <= 0  ) $star = 0;
			if ($rater_rating >= 0.5) $star = 1;
    		if ($rater_rating >= 1) $star = 2;
			if ($rater_rating >= 1.5) $star = 3;
			if ($rater_rating >= 2) $star = 4;
			if ($rater_rating >= 2.5) $star = 5;
			if ($rater_rating >= 3) $star = 6;
			if ($rater_rating >= 3.5) $star = 7;
			if ($rater_rating >= 4) $star = 8;
			if ($rater_rating >= 4.5) $star = 9;
			if ($rater_rating >= 5) $star = 10;
			if($order == 'rating_total_week' || $order == 'downloaded_week' || $order == 'viewed_week'){
			
				$song_HTML .= "<li><p class='right'><span class='rate s_".$star."'><span><span><span><span><span></span></span></span></span></span></span><span class='add_pl'><a href='#' onclick='$addp' title='Add to Playlist'></a></span></p><p class='title'><b>$i. </b><a href='$song_URL' title='$song_TITLE'>$song_TITLE1</a></p><p class='user'><p class='user'>Đăng bởi: <a href='$user_URL'>$user_NAME</a> | <a href='$cat_URL'>$cat_NAME</a> | Lượt nghe: $song_VIEWED | Người thích: $sumlike </p></li>";
			$i=$i+1;
			}
			else{
				if($song_VIEWED >= 10000 and $song_VIEWED <= 30000)
				{
					$song_HTML .= "<li><p class='right'><span class='rate s_".$star."'><span><span><span><span><span></span></span></span></span></span></span><span class='add_pl'><a href='#' onclick='$addp' title='Add to Playlist'></a></span></p><p class='title'><a href='$song_URL' title='$song_TITLE'>$song_TITLE1</a><img src='http://matxac.com/hot.gif' alt='HOT' /></p><p class='user'>Đăng bởi: <a href='$user_URL'>$user_NAME</a> | <a href='$cat_URL'>$cat_NAME</a> | Lượt nghe: $song_VIEWED | Người thích: $sumlike </p></li>";
				}
				else if($song_VIEWED >= 0 and $song_VIEWED <= 1000)
				{
					$song_HTML .= "<li><p class='right'><span class='rate s_".$star."'><span><span><span><span><span></span></span></span></span></span></span><span class='add_pl'><a href='#' onclick='$addp' title='Add to Playlist'></a></span></p><p class='title'><a href='$song_URL' title='$song_TITLE'>$song_TITLE1</a><img src='http://matxac.com/new.gif' alt='NEW' /></p><p class='user'>Đăng bởi: <a href='$user_URL'>$user_NAME</a> | <a href='$cat_URL'>$cat_NAME</a> | Lượt nghe: $song_VIEWED | Người thích: $sumlike </p></li>";
				}
				else {			
				$song_HTML .= "<li><p class='right'><span class='rate s_".$star."'><span><span><span><span><span></span></span></span></span></span></span><span class='add_pl'><a href='#' onclick='$addp' title='Add to Playlist'></a></span></p><p class='title'><a href='$song_URL' title='$song_TITLE'>$song_TITLE1</a></p><p class='user'>Đăng bởi: <a href='$user_URL'>$user_NAME</a> | <a href='$cat_URL'>$cat_NAME</a> | Lượt nghe: $song_VIEWED | Người thích: $sumlike </p></li>";
				}
			}
		}
	}
	else $song_HTML = '<li><p class="title">Chưa có !</p></li>';
include("templates/".$_SESSION['current_tpl']."/list.html");
}
if(!$show) {
	$sql_order = "ORDER BY m_id DESC";
	$num = $mysql->query("SELECT * FROM ".$tb_prefix."data $sql_where");
	$total = $mysql->num_rows($num);
	if (!$pg) $pg = 1;
	$m_per_page = m_get_config("media_per_page");
	// QUERY SQL
	$sql = "SELECT ".$fields." FROM ".$tb_prefix."data $sql_order LIMIT ".(($pg-1)*$m_per_page).",".$m_per_page;
	$query = $mysql->query($sql);
	if($mysql->num_rows($query)) {
		while($row = $mysql->fetch_array($query)) {
		switch ($row['m_type']) {
			case 1 : $media_type = 'music'; break;
			case 2 : $media_type = 'flash'; break;
			case 3 : $media_type = 'movie'; break;
		}
		$song_TYPE = "<img src='$tpl_link/img/media/type/$media_type.gif'>";
		$song_LYRIC = ($row['m_lyric'])?"<img src='$tpl_link/img/media/ok.gif'>":'';
		$song_ID = $row['m_id'];
		/*
		 * get sumlike 1 bai hat.
		 * 
		 */
		$sqls = "SELECT count(*) as sumlike FROM ".$tb_prefix."post WHERE post_media_id =" . $song_ID;
		$s = $mysql->fetch_array($mysql->query($sqls));
		$sumlike = $s['sumlike'];
		//end $sumlike ---------------------------------------------------------
		
		$user_ID = $row['m_poster'];
		$user_NAME =  m_get_data('USER',$user_ID);
		
		$song_TITLE = $row['m_title'];
		$singer_ID = $row['m_singer'];
		$singer_NAME = m_get_data('SINGER',$row['m_singer']);
		$song_VIEWED = $row['m_viewed'];
		$song_DOWNLOADED = $row['m_downloaded'];
		$cat_ID =  $row['m_cat'];
		$cat_NAME =  m_get_data('CAT',$cat_ID);
		$cat_URL    = url_link($cat_ID,$cat_NAME,'Cat');		
		$song_URL = url_link($song_ID,$song_TITLE,'nhac-san');
		$singer_URL = url_link($singer_ID,$singer_NAME,'Singer');
		$link  = (m_get_config('url_type') == 0)?$mainURL."/index.php?pg=":$mainURL."/Page/";
		$title = "Danh sách nhạc";
		$view_pages = m_viewpages($total,$m_per_page,$pg,$link);
		if($user_NAME == '')
			{
				$user_NAME = 'DJ Other';
			}
			$addp = 'addToPlaylist('.$song_ID.');return false;';	
			if (!$isLoggedIn){
				$addp = 'alert("Yêu cầu đăng nhập")';
			}			
		$user_URL	= url_link($user_ID,$user_NAME,'User');
		$m_bitrate = $row['m_bitrate'];
		$m_playtime = $row['m_playtime'];
		$m_rate = $row['m_rating'];
			$m_rate_total = $row['m_rating_total'];
			if ($m_rate =='0') $current_star = 0;

			else $rater_rating = $m_rate / $m_rate_total;
			if ($rater_rating <= 0  ) $star = 0;
			if ($rater_rating >= 0.5) $star = 1;
    		if ($rater_rating >= 1) $star = 2;
			if ($rater_rating >= 1.5) $star = 3;
			if ($rater_rating >= 2) $star = 4;
			if ($rater_rating >= 2.5) $star = 5;
			if ($rater_rating >= 3) $star = 6;
			if ($rater_rating >= 3.5) $star = 7;
			if ($rater_rating >= 4) $star = 8;
			if ($rater_rating >= 4.5) $star = 9;
			if ($rater_rating >= 5) $star = 10;
			$song_TITLE1 = $song_TITLE;
			if($order == 'rating_total_week' || $order == 'downloaded_week' || $order == 'viewed_week'){
			
				$song_HTML .= "<li><p class='right'><span class='rate s_".$star."'><span><span><span><span><span></span></span></span></span></span></span><span class='add_pl'><a href='#' onclick='$addp' title='Add to Playlist'></a></span></p><p class='title'><b>$i. </b><a href='$song_URL' title='$song_TITLE'>$song_TITLE1</a></p><p class='user'>Đăng bởi: <a href='$user_URL'>$user_NAME</a> | <a href='$cat_URL'>$cat_NAME</a> | Lượt nghe: $song_VIEWED | Người thích: $sumlike </p></li>";
			$i=$i+1;
			}
			else{
			
				if($song_VIEWED >= 15000 and $song_VIEWED <= 30000)
				{
					$song_HTML .= "<li><p class='right'><span class='rate s_".$star."'><span><span><span><span><span></span></span></span></span></span></span><span class='add_pl'><a href='#' onclick='$addp' title='Add to Playlist'></a></span></p><p class='title'><a href='$song_URL' title='$song_TITLE'>$song_TITLE1</a><img src='http://matxac.com/hot.gif' alt='HOT' /></p><p class='user'>Đăng bởi: <a href='$user_URL'>$user_NAME</a> | <a href='$cat_URL'>$cat_NAME</a> | Lượt nghe: $song_VIEWED | Người thích: $sumlike </p></li>";
				}
				else if($song_VIEWED >= 0 and $song_VIEWED <= 1000)
				{
					$song_HTML .= "<li><p class='right'><span class='rate s_".$star."'><span><span><span><span><span></span></span></span></span></span></span><span class='add_pl'><a href='#' onclick='$addp' title='Add to Playlist'></a></span></p><p class='title'><a href='$song_URL' title='$song_TITLE'>$song_TITLE1</a><img src='http://matxac.com/new.gif' alt='NEW' /></p><p class='user'>Đăng bởi: <a href='$user_URL'>$user_NAME</a> | <a href='$cat_URL'>$cat_NAME</a> | Lượt nghe: $song_VIEWED | Người thích: $sumlike </p></li>";
				}
				else {			
				$song_HTML .= "<li><p class='right'><span class='rate s_".$star."'><span><span><span><span><span></span></span></span></span></span></span><span class='add_pl'><a href='#' onclick='$addp' title='Add to Playlist'></a></span></p><p class='title'><a href='$song_URL' title='$song_TITLE'>$song_TITLE1</a></p><p class='user'>Đăng bởi: <a href='$user_URL'>$user_NAME</a> | <a href='$cat_URL'>$cat_NAME</a> | Lượt nghe: $song_VIEWED | Người thích: $sumlike </p></li>";
				}
			}		
		}
	}
	else $song_HTML = '<li><p class="title">Chưa có !</p></li>';
include("templates/".$_SESSION['current_tpl']."/list.html");
}
?>